package com.ztgf.shopping.api.client.shop;

import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ztgf.commodity.models.service.ICommodityInfoService;
import com.ztgf.commodity.models.vo.CommodityEvaluationScoreVO;
import com.ztgf.commodity.models.vo.CommodityInfoRankVO;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.pay.models.bo.PayUserSaveBO;
import com.ztgf.pay.models.dto.PayUserDTO;
import com.ztgf.pay.models.service.IPayUserService;
import com.ztgf.shopping.models.bo.shop.*;
import com.ztgf.shopping.models.dto.shop.ShopBlacklistDTO;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.shop.IShopBlacklistService;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.shopping.models.service.shop.IShopQiyuesuoInfoService;
import com.ztgf.shopping.models.service.template.ITempletInfoService;
import com.ztgf.shopping.models.vo.shop.ShopInfoUserVO;
import com.ztgf.shopping.models.vo.shop.ShopInfoVO;
import com.ztgf.shopping.models.vo.shop.ShopUpdateAuditVO;
import com.ztgf.user.models.service.IUserLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 店铺表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-28 14:35:38
 */

@RestController
@RequestMapping("/api/shopInfo")
@Slf4j
@Api(value = "ShopInfoApiController", tags = "店铺表Api接口")
public class ShopInfoApiController {

    @DubboReference
    private IShopInfoService shopInfoService;

    @DubboReference
    private ITempletInfoService iTempletInfoService;

    @DubboReference
    private IShopBlacklistService iShopBlacklistService;
    @DubboReference
    private IShopQiyuesuoInfoService iShopQiyuesuoInfoService;
    @DubboReference
    private ICommodityInfoService iCommodityInfoService;
    @DubboReference
    private IUserLoginService userLoginService;
    @DubboReference
    private IPayUserService iPayUserService;

//    @ApiOperation(value = "根据店铺id获取模板信息",notes = "根据店铺id获取模板信息")
//    @PostMapping("/getTemListInfoByShopId")
//    public Result getTemListInfoByShopId(Long shopId){
//        if(null == shopId){
//            return new Result<>(CodeEnum.FAIL_100002 );
//        }
//        List<TempletInfoDTO> templetInfoDTOList = iTempletInfoService.getShopTemListByShopId(shopId);
//        return new Result<>(templetInfoDTOList);
//    }

    @ApiOperation(value = "注册卖家店铺", notes = "注册卖家店铺")
    @PostMapping("/update")
    public Result update(ShopInfoUpdateBO updateBO) {
        if (StringUtils.isEmpty(updateBO.getShopName()) || StringUtils.isEmpty(updateBO.getDomain())) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        //验证是否已经加入卖家黑名单
        //黑名单类型 0用户黑名单，1店铺黑名单
        ShopBlacklistDTO shopBlacklistDTO = iShopBlacklistService.getByShopId(updateBO.getId(), ShoppingCodeEnum.BLACKLIST_OPEN.getCode());
        if (shopBlacklistDTO != null) {
            return new Result<>(CodeEnum.FAIL_100038);
        }

        //验证店铺名重复
        if (shopInfoService.getbyShopName(updateBO.getShopName())) {
            return new Result<>(CodeEnum.FAIL_AddUser_3008);
        }
        //验证二级域名重复
        if (shopInfoService.getbyDomain(updateBO.getDomain())) {
            return new Result<>(CodeEnum.FAIL_AddUser_3009);
        }

        //验证是否已经处于开店状态
        ShopInfoDTO shopInfoDTO = shopInfoService.selectById(updateBO.getId());
        if (shopInfoDTO.getSellerStatus() != ShoppingCodeEnum.AUDIT_NULL.getCode()) {
            return new Result<>(CodeEnum.FAIL_AddUser_3010);
        }

        boolean flag = shopInfoService.update(updateBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<ShopInfoVO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        ShopInfoVO vo = shopInfoService.selectVoById(id);
        if (vo.getId() == null) {
            return new Result<>(CodeEnum.FAIL);
        } else {
            return new Result<>(vo);
        }
    }

    @PostMapping("/getDomain")
    @ApiOperation(value = "根据店铺二级域名查找店铺", notes = "根据店铺二级域名查找店铺")
    public Result<ShopInfoVO> getShopInfoByDomain(@RequestParam("domain") String domain) {
        if (domain == null || domain.equals("")) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        ShopInfoVO vo = shopInfoService.selectVoByDomain(domain);
        if (vo.getId() == null) {
            return new Result<>(CodeEnum.FAIL);
        } else {
            return new Result<>(vo);
        }
    }

    @PostMapping("/updateAddress")
    @ApiOperation(value = "修改店铺地址", notes = "修改店铺地址")
    public Result updateAddress(@RequestParam("id") Long id, String address) {
        if (id == null || address == null || "".equals(address)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        ShopInfoDTO dto = new ShopInfoDTO();
        dto.setId(id);
        dto.setAddress(address);

        boolean flag = shopInfoService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL);
        }
    }

    @PostMapping("/getByUserId")
    @ApiOperation(value = "根据用户id查找", notes = "根据用户id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<ShopInfoVO> getByUserId(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        ShopInfoVO vo = shopInfoService.selectVoByUserId(id);
        if (vo.getId() == null) {
            return new Result<>(CodeEnum.FAIL);
        } else {
            return new Result<>(vo);
        }
    }

    @PostMapping("/getScore")
    @ApiOperation(value = "根据id获取店铺评分", notes = "根据id获取店铺评分")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<CommodityEvaluationScoreVO> getScore(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        CommodityEvaluationScoreVO vo = shopInfoService.getScore(id);
        return new Result<>(vo);
    }

    @PostMapping("/getOpenQualification")
    @ApiOperation(value = "判断开店资格", notes = "判断开店资格")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result getOpenQualification(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        //验证是否已经加入卖家黑名单
        //黑名单类型 0用户黑名单，1店铺黑名单
        ShopBlacklistDTO shopBlacklistDTO = iShopBlacklistService.getByShopId(id, ShoppingCodeEnum.BLACKLIST_OPEN.getCode());
        if (shopBlacklistDTO != null) {
            return new Result<>(CodeEnum.FAIL_100038);
        }
        //验证有无电子签章
        ShopQiyuesuoAuthStateInfoBO shopQiyuesuoAuthStateInfoBO = iShopQiyuesuoInfoService.getByQiyuesuoState(id);
        if (!shopQiyuesuoAuthStateInfoBO.isAuthState()) {
            return new Result<>(CodeEnum.FAIL_100039.getCode(), shopQiyuesuoAuthStateInfoBO.getResult(), shopQiyuesuoAuthStateInfoBO.getAuthUrl());
        }

        //验证是否开户成功
        ShopInfoDTO shopInfoDTO = shopInfoService.selectById(id);
        if (shopInfoDTO.getCiccStatus().equals(0)) {
            return new Result<>(CodeEnum.FAIL_200029);
        }

        return new Result<>();
    }

    @ApiOperation(value = "店铺列表", notes = "店铺列表")
    @PostMapping("/queryApiPageList")
    public Result<Page<ShopInfoDTO>> queryApiPageList(ShopInfoApiBO bo) {
        Page<ShopInfoDTO> dtos = shopInfoService.queryApiPageList(bo);
        return new Result<>(dtos);
    }

    @PostMapping("/getCommoditySalesRank")
    @ApiOperation(value = "获取店铺商品销量排名", notes = "获取店铺商品销量排名")
    public Result<List<CommodityInfoRankVO>> getCommoditySalesRank(Long id) {
        List<CommodityInfoRankVO> vos = iCommodityInfoService.getCommoditySalesRank(id);
        return new Result<>(vos);
    }

    @ApiOperation(value = "修改支付手机号", notes = "修改支付手机号")
    @PostMapping("/updatePayMobile")
    public Result updatePayMobile(ShopInfoUpdatePayMobileBO bo) {
        if (bo.getId() == null || StringUtils.isEmpty(bo.getMobile()) || StringUtils.isEmpty(bo.getCaptcha())) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        //验证码0注册验证码，1登录验证码，2修改密码验证码，3更改绑定手机验证码，4支付验证码，5保证金修改验证码, 6更改绑定支付手机号
        if (!userLoginService.getSmsCaptcha(bo.getMobile(), bo.getCaptcha(), 6)) {
            return new Result<>(CodeEnum.FAIL_Login_1004);
        }

        //验证码0注册验证码，1登录验证码，2修改密码验证码，3更改绑定手机验证码，4支付验证码，5保证金修改验证码, 6更改绑定支付手机号
        if (!userLoginService.getSmsCaptcha(bo.getNewMobile(), bo.getNewCaptcha(), 6)) {
            return new Result<>(CodeEnum.FAIL_Login_1004);
        }

        ShopInfoDTO dto = new ShopInfoDTO();
        dto.setId(bo.getId());
        dto.setPayMobile(bo.getNewMobile());

        boolean flag = shopInfoService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL);
        }
    }

    @ApiOperation(value = "修改支付验证", notes = "修改支付验证")
    @PostMapping("/updatePayVerification")
    public Result updatePayVerification(ShopInfoUpdatePayVerification bo) {
        if (bo.getId() == null || bo.getStatus() == null || StringUtils.isEmpty(bo.getMobile()) || StringUtils.isEmpty(bo.getCaptcha())) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        //验证码0注册验证码，1登录验证码，2修改密码验证码，3更改绑定手机验证码，4支付验证码，5保证金修改验证码, 6更改绑定支付手机号
        if (!userLoginService.getSmsCaptcha(bo.getMobile(), bo.getCaptcha(), 7)) {
            return new Result<>(CodeEnum.FAIL_Login_1004);
        }

        ShopInfoDTO dto = new ShopInfoDTO();
        dto.setId(bo.getId());
        dto.setPayVerification(bo.getStatus());

        boolean flag = shopInfoService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL);
        }
    }

    @ApiOperation(value = "中金重新开户", notes = "中金重新开户")
    @PostMapping("/reOpenCicc")
    public Result reOpenCicc(PayUserSaveBO bo) {
        if (bo.getUserId() == null || StringUtils.isEmpty(bo.getLegalCredentialsNo()) || StringUtils.isEmpty(bo.getUserMobile())) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        ShopInfoDTO shopInfoDTO = shopInfoService.selectById(bo.getUserId());
        if (shopInfoDTO == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        bo.setUserId(shopInfoDTO.getId());
        bo.setUserName(shopInfoDTO.getName());
        bo.setType(1);
        bo.setLegalName(shopInfoDTO.getLegalPerson());
        bo.setLegalCredentialsType(shopInfoDTO.getIdCardType());
        bo.setLegalCredentialsNo(shopInfoDTO.getIdCard());
        bo.setBusinessLicenseNo(shopInfoDTO.getCreditCode());
        bo.setUserAddress(shopInfoDTO.getAddress());

        Result result = iPayUserService.add(bo);
        ShopInfoDTO newDTO = new ShopInfoDTO();
        newDTO.setId(bo.getUserId());
        if (result.getCode() == 0) {
            newDTO.setCiccStatus(1);//中金开户状态 0未开户，1已开户
            ObjectMapper objectMapper = new ObjectMapper();
            PayUserDTO dto = objectMapper.convertValue(result.getData(), PayUserDTO.class);
            newDTO.setZjMoneyNo(dto.getZjMoneyNo());
            newDTO.setZjBankName(dto.getZjBankName());
            shopInfoService.updateById(newDTO);
        }

        return result;
    }

    @PostMapping("/getUser")
    @ApiOperation(value = "获取根据店铺ID获取用户信息", notes = "获取根据店铺ID获取用户信息")
    public Result<ShopInfoUserVO> getUser(Long id, Integer type) {
        Integer imType = type;
        if (type == null) {
            imType = 0;
        }
        ShopInfoUserVO vo = shopInfoService.getUser(id, imType);
        return new Result<>(vo);
    }

    @ApiOperation(value = "开店，资质变更，新增品牌，新增类目", notes = "开店，资质变更，新增品牌，新增类目")
    @PostMapping(value = "/registeredShop")
    public Result registeredShop(String shopVO) {
        if (StringUtils.isEmpty(shopVO)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return shopInfoService.saveRegisteredShop(shopVO);
    }

    @PostMapping("/getOpenShopMessage")
    @ApiOperation(value = "获取最近一次开店审核通过的信息", notes = "获取最近一次开店审核通过的信息")
    public Result<?> getOpenShopMessage(Long shopId, Integer type) {
        if (shopId == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        List<ShopUpdateAuditVO> list = shopInfoService.getOpenShopMessage(shopId, type);
        Result<?> result = new Result<>(list);
        return new Result<>(result);
    }

    @PostMapping(value = "/updateShopTypeCommit")
    @ApiOperation(value = "店铺类型变更提交", notes = "店铺类型变更提交")
    public Result<?> updateShopTypeCommit(String shopVO) {
        if (CustomizeUtil.strIsNull(shopVO)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return shopInfoService.updateShopTypeCommit(shopVO);
    }

    @PostMapping(value = "/getCheckUpdateShop")
    @ApiOperation(value = "检查店铺是否可以变更", notes = "检查店铺是否可以变更")
    public Result<?> getCheckUpdateShop(Long shopId) {
        Map<String, Integer> checkUpdateShop = shopInfoService.getCheckUpdateShop(shopId);
        return new Result<>(checkUpdateShop);
    }
}
